﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using Maticsoft.DBUtility;//Please add references
namespace hcrainMvc.DAL
{
	/// <summary>
	/// 数据访问类:hc_de_list
	/// </summary>
	public partial class hc_de_list
	{
		public hc_de_list()
		{}
		#region  BasicMethod



		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(hcrainMvc.Model.hc_de_list model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into hc_de_list(");
			strSql.Append("fid,listname,type,mid,field,title,width,sortable,checkbox,align,formatter,orderbythis)");
			strSql.Append(" values (");
			strSql.Append("@fid,@listname,@type,@mid,@field,@title,@width,@sortable,@checkbox,@align,@formatter,@orderbythis)");
			MySqlParameter[] parameters = {
					new MySqlParameter("@fid", MySqlDbType.Double),
					new MySqlParameter("@listname", MySqlDbType.VarChar,50),
					new MySqlParameter("@type", MySqlDbType.VarChar,50),
					new MySqlParameter("@mid", MySqlDbType.Double),
					new MySqlParameter("@field", MySqlDbType.VarChar,50),
					new MySqlParameter("@title", MySqlDbType.VarChar,50),
					new MySqlParameter("@width", MySqlDbType.VarChar,50),
					new MySqlParameter("@sortable", MySqlDbType.VarChar,50),
					new MySqlParameter("@checkbox", MySqlDbType.VarChar,50),
					new MySqlParameter("@align", MySqlDbType.VarChar,50),
					new MySqlParameter("@formatter", MySqlDbType.VarChar,250),
					new MySqlParameter("@orderbythis", MySqlDbType.Int32,11)};
			parameters[0].Value = model.fid;
			parameters[1].Value = model.listname;
			parameters[2].Value = model.type;
			parameters[3].Value = model.mid;
			parameters[4].Value = model.field;
			parameters[5].Value = model.title;
			parameters[6].Value = model.width;
			parameters[7].Value = model.sortable;
			parameters[8].Value = model.checkbox;
			parameters[9].Value = model.align;
			parameters[10].Value = model.formatter;
			parameters[11].Value = model.orderbythis;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(hcrainMvc.Model.hc_de_list model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update hc_de_list set ");
			strSql.Append("fid=@fid,");
			strSql.Append("listname=@listname,");
			strSql.Append("type=@type,");
			strSql.Append("mid=@mid,");
			strSql.Append("field=@field,");
			strSql.Append("title=@title,");
			strSql.Append("width=@width,");
			strSql.Append("sortable=@sortable,");
			strSql.Append("checkbox=@checkbox,");
			strSql.Append("align=@align,");
			strSql.Append("formatter=@formatter,");
			strSql.Append("orderbythis=@orderbythis");
			strSql.Append(" where ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@fid", MySqlDbType.Double),
					new MySqlParameter("@listname", MySqlDbType.VarChar,50),
					new MySqlParameter("@type", MySqlDbType.VarChar,50),
					new MySqlParameter("@mid", MySqlDbType.Double),
					new MySqlParameter("@field", MySqlDbType.VarChar,50),
					new MySqlParameter("@title", MySqlDbType.VarChar,50),
					new MySqlParameter("@width", MySqlDbType.VarChar,50),
					new MySqlParameter("@sortable", MySqlDbType.VarChar,50),
					new MySqlParameter("@checkbox", MySqlDbType.VarChar,50),
					new MySqlParameter("@align", MySqlDbType.VarChar,50),
					new MySqlParameter("@formatter", MySqlDbType.VarChar,250),
					new MySqlParameter("@orderbythis", MySqlDbType.Int32,11)};
			parameters[0].Value = model.fid;
			parameters[1].Value = model.listname;
			parameters[2].Value = model.type;
			parameters[3].Value = model.mid;
			parameters[4].Value = model.field;
			parameters[5].Value = model.title;
			parameters[6].Value = model.width;
			parameters[7].Value = model.sortable;
			parameters[8].Value = model.checkbox;
			parameters[9].Value = model.align;
			parameters[10].Value = model.formatter;
			parameters[11].Value = model.orderbythis;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete()
		{
			//该表无主键信息，请自定义主键/条件字段
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from hc_de_list ");
			strSql.Append(" where ");
			MySqlParameter[] parameters = {
			};

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public hcrainMvc.Model.hc_de_list GetModel()
		{
			//该表无主键信息，请自定义主键/条件字段
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select fid,listname,type,mid,field,title,width,sortable,checkbox,align,formatter,orderbythis from hc_de_list ");
			strSql.Append(" where ");
			MySqlParameter[] parameters = {
			};

			hcrainMvc.Model.hc_de_list model=new hcrainMvc.Model.hc_de_list();
			DataSet ds=DbHelperMySQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public hcrainMvc.Model.hc_de_list DataRowToModel(DataRow row)
		{
			hcrainMvc.Model.hc_de_list model=new hcrainMvc.Model.hc_de_list();
			if (row != null)
			{
					//model.fid=row["fid"].ToString();
				if(row["listname"]!=null)
				{
					model.listname=row["listname"].ToString();
				}
				if(row["type"]!=null)
				{
					model.type=row["type"].ToString();
				}
					//model.mid=row["mid"].ToString();
				if(row["field"]!=null)
				{
					model.field=row["field"].ToString();
				}
				if(row["title"]!=null)
				{
					model.title=row["title"].ToString();
				}
				if(row["width"]!=null)
				{
					model.width=row["width"].ToString();
				}
				if(row["sortable"]!=null)
				{
					model.sortable=row["sortable"].ToString();
				}
				if(row["checkbox"]!=null)
				{
					model.checkbox=row["checkbox"].ToString();
				}
				if(row["align"]!=null)
				{
					model.align=row["align"].ToString();
				}
				if(row["formatter"]!=null)
				{
					model.formatter=row["formatter"].ToString();
				}
				if(row["orderbythis"]!=null && row["orderbythis"].ToString()!="")
				{
					model.orderbythis=int.Parse(row["orderbythis"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select fid,listname,type,mid,field,title,width,sortable,checkbox,align,formatter,orderbythis ");
			strSql.Append(" FROM hc_de_list ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM hc_de_list ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.fid desc");
			}
			strSql.Append(")AS Row, T.*  from hc_de_list T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			MySqlParameter[] parameters = {
					new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@PageSize", MySqlDbType.Int32),
					new MySqlParameter("@PageIndex", MySqlDbType.Int32),
					new MySqlParameter("@IsReCount", MySqlDbType.Bit),
					new MySqlParameter("@OrderType", MySqlDbType.Bit),
					new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
					};
			parameters[0].Value = "hc_de_list";
			parameters[1].Value = "fid";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

